package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.eventbus.SearchPenpalArticleMode;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.penpal.adapter.PenpalSearchArticleTypePageAdapter;
import com.rongshu.widget.TabPageIndicator;
import com.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/1.
 * description：笔友会-搜索文章/帖子
 */
public class PenpalSearchArticleActivity extends BaseActivity {


    @BindView(R.id.search_left_tv)
    public EditText searchLeftTv;
    @BindView(R.id.indicator)
    TabPageIndicator indicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private SearchPenpalArticleMode mode;
    private PenpalSearchArticleTypePageAdapter adapter;


    @Override
    protected int initLayout() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initView() {
        searchLeftTv.setHint("请输入关键字");
        Intent intent = getIntent();
        if (intent != null) {
            mode = (SearchPenpalArticleMode) intent.getSerializableExtra(Constant.INTENT_DATA);
            searchLeftTv.setText(mode.getContent());
        }
        adapter = new PenpalSearchArticleTypePageAdapter(getSupportFragmentManager(), mode);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        indicator.setViewPager(viewPager);
        setTabPagerIndicator();
        searchLeftTv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    showKeyboard(false);
                    String msg = v.getText().toString();
                    SearchPenpalArticleMode searchMode = new SearchPenpalArticleMode();
                    searchMode.setContent(msg);
                    EventBus.getDefault().post(searchMode);
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    private void setTabPagerIndicator() {
        indicator.setIndicatorMode(TabPageIndicator.IndicatorMode.MODE_WEIGHT_NOEXPAND_SAME);// 设置模式，一定要先设置模式
        indicator.setTextColor(ContextCompat.getColor(this, R.color.color_2c2c2c));
        indicator.setIndicatorColor(ContextCompat.getColor(this, R.color.color_3CC051));// 设置底部导航线的颜色
        indicator.setTextColorSelected(ContextCompat.getColor(this, R.color.color_3CC051));// 设置tab标题选中的颜色
        indicator.setTextSize(DensityUtil.sp2px(this, 16));// 设置字体大小
    }

    @OnClick({R.id.search_right_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_right_tv:
                showKeyboard(false);
                finish();
                break;
        }
    }
}
